<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;


class Weal extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%weal}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'id',
                'num',
            ],
            'update_self_info'=>[
                'username',
                'head_img',
                'sex',
                'city_id',
                'province_id',
//                'birthday',
            ]
        ];

    }

    public static function cutNum($id)
    {
        $model = static::findOne(['id' => $id]);
        if (empty($model)) {
            return false;
        }
        $res = $model->toArray();
        if ($res['num'] <= 0) {
            return false;
        }
        $model->num = $res['num'] - 1;
        return $model->save();
    }

    /**
     * @inheritdoc
     */
    public static function selectFieldData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('weal');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['id'])){
            $query->andWhere(['id' => $data['id']]);
        }
        if(!empty($data['status'])){
            $query->andWhere(['status' => $data['status']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        foreach ($result as $k => $v){
            $result[$k]['img'] = explode(",",$v['images']);
        }
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function findData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('weal');
        $query->where(['deleted_at' => null]);
        if(!empty($data['id'])){
            $query->andWhere(['id' => $data['id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryOne();
        $result['images_arr'] = explode(",",$result['images']);
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function findOneData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('weal');
        $query->where(['deleted_at' => null]);
        if(!empty($data['id'])){
            $query->andWhere(['id' => $data['id']]);
        }

        $command = $query->createCommand();
        $result = $command->queryOne();
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function findCity($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('district');
        $query->where(['deleted_at' => null]);
        if(!empty($data['data'])){
            $query->andWhere(['front_code' => $data['data']]);
        }

        $command = $query->createCommand();
        $result = $command->queryOne();
        return $result;
    }



}
